﻿#region Copyright
//Copyright 2012 Phelon Aka. Jon H.
/*
    This file is part of BGBuddy - Ultimate PVP Suite.

    BGBuddy - Ultimate PVP Suite is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    BGBuddy - Ultimate PVP Suite is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Foobar.  If not, see <http://www.gnu.org/licenses/>
 */
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Styx.Plugins;
using Styx;
using Styx.Helpers;
using Styx.CommonBot.POI;
using Styx.WoWInternals.WoWObjects;
using Styx.WoWInternals;
using Styx.Pathing;
using Styx.CommonBot;
using Styx.Common.Helpers;

namespace UltimatePvPSuite
{
    public static class Consumables
    {
        //private static readonly Stopwatch TrinketTimer = new Stopwatch();
        //private static readonly Stopwatch PotionTimer = new Stopwatch();
        //private static readonly Stopwatch BannerTimer = new Stopwatch();
        public static string LuaGetReturnValueString(string lua) { return Lua.GetReturnValues(lua, "stuff.lua")[0]; }
        private static readonly WaitTimer ConsumableTimer = new WaitTimer(TimeSpan.FromMilliseconds(5000));

        public static bool Pulse()
        {
            if (Convert.ToInt32(UltimatePvPSuite.UseConsumablePercent) >= StyxWoW.Me.HealthPercent && ConsumableTimer.IsFinished)
            {
                if (UseHealthStone())
                {
                    ConsumableTimer.Reset();
                    return true;
                }

                if (UseHealthPots())
                {
                    ConsumableTimer.Reset();
                    return true;
                }

                if (UsePvPHealthPots())
                {
                    ConsumableTimer.Reset();
                    return true;
                }

                if (UsePvPBanner())
                {
                    ConsumableTimer.Reset();
                    return true;
                }

                if (UseHealthTrinkets())
                {
                    ConsumableTimer.Reset();
                    return true;
                }
            }
            return false;
        }
        //Consume's Health Stone
        private static bool UseHealthStone()
        {
            if (UltimatePvPSuite.EnableUseHealthStone)
            {
                foreach (WoWItem HealthStone in StyxWoW.Me.BagItems)
                {
                    if (HealthStone.Name.Contains("Healthstone"))
                    {
                        Logger.Write("Using HealthStone.");
                        HealthStone.Use();
                        //PotionTimer.Reset();
                        //PotionTimer.Start();
                        return true;
                    }
                }
            }
            return false;
        }

        //Use Health Potions
        private static bool UseHealthPots()
        {
            if (UltimatePvPSuite.EnableUseHealthPotions)
            {
                //if (!PotionTimer.IsRunning || PotionTimer.ElapsedMilliseconds > (120 * 1000))
                //{
                    WoWItem HealthPot = HealthPotion();
                    if (HealthPot != null)
                    {
                        string luacode = String.Format("return GetItemCooldown(\"{0}\")", HealthPot.Entry);
                        if (LuaGetReturnValueString(luacode) == "0")
                        {
                            Logger.Write("Using Health Potion.");
                            HealthPot.Use();
                            //PotionTimer.Reset();
                            //PotionTimer.Start();
                            return true;
                        }
                    }
                //}
            }
            return false;
        }

        //Use PVP Potions
        private static bool UsePvPHealthPots()
        {
            if (UltimatePvPSuite.EnableUsePvPHealthPotions)
            {
                //if (!PotionTimer.IsRunning || PotionTimer.ElapsedMilliseconds > (120 * 1000))
                //{
                    WoWItem PvPHealthPot = PvPHealthPotion();
                    if (PvPHealthPot != null)
                    {
                        string luacode = String.Format("return GetItemCooldown(\"{0}\")", PvPHealthPot.Entry);
                        if (LuaGetReturnValueString(luacode) == "0")
                        {
                            Logger.Write("PvP Health Potion.");
                            PvPHealthPot.Use();
                            //PotionTimer.Reset();
                            //PotionTimer.Start();
                            return true;
                        }
                    }
                //}
            }
            return false;
        }

        //Uses PvP Banner
        private static bool UsePvPBanner()
        {
            if (UltimatePvPSuite.EnableUsePvPBanner)
            {
                //if (!BannerTimer.IsRunning || BannerTimer.ElapsedMilliseconds > (600 * 1000))
                //{
                    foreach (WoWItem banner in StyxWoW.Me.BagItems)
                    {
                        if (banner.Name.Contains("Battle Standard"))
                        {
                            string luacode = String.Format("return GetItemCooldown(\"{0}\")", banner.Entry);
                            if (LuaGetReturnValueString(luacode) == "0")
                            {
                                Logger.Write("Using Battle Standard.");
                                banner.Use();
                                //PotionTimer.Reset();
                                //PotionTimer.Start();
                                return true;
                            }
                        }
                    }
                //}
            }
            return false;
        }
        
        //Use Health Trinkets
        private static bool UseHealthTrinkets()
        {
            if (UltimatePvPSuite.EnableUseHealthTrinket)
            {
                //if (!TrinketTimer.IsRunning || TrinketTimer.ElapsedMilliseconds > (120 * 1000))
                //{
                    string[] Trinkets = { "Emblem of Tenacity", "Emblem of Meditation", "Emblem of Cruelty", "Emblem of Profiency" };
                    foreach (string trinket in Trinkets)
                    {
                        if (trinket != null)
                        {
                            if (StyxWoW.Me.Inventory.Equipped.Trinket1.Name.Contains(trinket))
                            {
                                string luacode = String.Format("return GetItemCooldown(\"{0}\")", StyxWoW.Me.Inventory.Equipped.Trinket1.Entry);
                                if (LuaGetReturnValueString(luacode) == "0")
                                {
                                    Logger.Write("HP Trinket1.");
                                    StyxWoW.Me.Inventory.Equipped.Trinket1.Use();
                                    //TrinketTimer.Reset();
                                    //TrinketTimer.Start();
                                    return true;
                                }
                            }
                            if (StyxWoW.Me.Inventory.Equipped.Trinket2.Name.Contains(trinket))
                            {
                                string luacode = String.Format("return GetItemCooldown(\"{0}\")", StyxWoW.Me.Inventory.Equipped.Trinket2.Entry);
                                if (LuaGetReturnValueString(luacode) == "0")
                                {
                                    Logger.Write("HP Trinket2.");
                                    StyxWoW.Me.Inventory.Equipped.Trinket2.Use();
                                    //TrinketTimer.Reset();
                                    //TrinketTimer.Start();
                                    return true;
                                }
                            }
                        }
                    }
                //}
            }
            return false;
        }

        //Use Flasks
        private static bool UseFlasks()
        {
            return false;
        }

        #region Health and Mana Pots List
        private static WoWItem PvPHealthPotion()
        {
            foreach (WoWItem Item in StyxWoW.Me.BagItems)
            {
                switch (Item.Entry)
                {
                    case 67415: return Item; //Draught of War
                    case 63144: return Item; //Baradin's Wardens Healing Potion
                    case 64994: return Item; //Hellscream's Reach Healing Potion
                    case 31838: return Item; //Major Combat Healing Potion
                    case 31839: return Item; //Major Combat Healing Potion
                    case 31852: return Item; //Major Combat Healing Potion
                    case 31853: return Item; //Major Combat Healing Potion
                    case 17384: return Item; //Major Healing Draught
                    case 17349: return Item; //Superior Healing Draught
                }
            }
            return null;
        }

        private static WoWItem HealthPotion()
        {
            foreach (WoWItem Item in StyxWoW.Me.BagItems)
            {
                switch (Item.Entry)
                {
                    case 76097: return Item; //Master Healing Potion
                    case 80040: return Item; //Endless Master Healing Potion
                    case 57193: return Item; //Mighty Rejuvenation Potion
                    case 57191: return Item; //Mythical Healing Potion
                    case 63300: return Item; //Rogue's Draught
                    case 33447: return Item; //Runic Healing Potion
                    case 40077: return Item; //Crazy Alchemist's Potion
                    case 40081: return Item; //Potion of Nightmares
                    case 40087: return Item; //Powerful Rejuvenation Potion
                    case 41166: return Item; //Runic Healing Injector
                    case 43569: return Item; //Endless Healing Potion
                    case 22850: return Item; //Super Rejuvenation Potion
                    case 34440: return Item; //Mad Alchemist's Potion
                    case 39671: return Item; //Resurgent Healing Potion
                    case 32784: return Item; //Red Ogre Brew
                    case 32910: return Item; //Red Ogre Brew Special
                    case 31676: return Item; //Fel Regeneration Potion
                    case 23822: return Item; //Healing Potion Injector
                    case 33092: return Item; //Healing Potion Injector
                    case 22829: return Item; //Super Healing Potion
                    case 32763: return Item; //Rulkster's Secret Sauce
                    case 32904: return Item; //Cenarion Healing Salve
                    case 32905: return Item; //Bottled Nethergon Vapor
                    case 32947: return Item; //Auchenai Healing Potion
                    case 39327: return Item; //Noth's Special Brew
                    case 43531: return Item; //Argent Healing Potion
                    case 18253: return Item; //Major Rejuvenation Potion
                    case 28100: return Item; //Volatile Healing Potion
                    case 33934: return Item; //Crystal Healing Potion
                    case 13446: return Item; //Major Healing Potion
                    case 3928: return Item; //Superior Healing Potion
                    case 9144: return Item; //Wildvine Potion
                    case 12190: return Item; //Dreamless Sleep Potion
                    case 18839: return Item; //Combat Healing Potion
                    case 1710: return Item; //Greater Healing Potion
                    case 929: return Item; //Healing Potion
                    case 4596: return Item; //Discolored Healing Potion
                    case 858: return Item; //Lesser Healing Potion
                    case 118: return Item; //Minor Healing Potion
                }
            }
            return null;
        }

        private static WoWItem ManaPotion()
        {
            foreach (WoWItem Item in StyxWoW.Me.BagItems)
            {
                switch (Item.Entry)
                {
                    case 76098: return Item; // Master Mana Potion
                    case 63145: return Item; // Baradin's Wardens Mana Potion
                    case 64993: return Item; // Hellscream's Reach Mana Potion
                    case 57193: return Item; // Mighty Rejuvenation Potion
                    case 57192: return Item; // Mythical Mana Potion
                    case 67145: return Item; // Draught of War
                    case 33448: return Item; //Runic Mana Potion
                    case 40077: return Item; // Crazy Alchemist's Potion
                    case 40081: return Item; // Potion of Nightmares
                    case 40087: return Item; // Powerful Rejuvenation Potion
                    case 42545: return Item; // Runic Mana Injector
                    case 43570: return Item; // Endless Mana Potion
                    case 22850: return Item; // Super Rejuvenation Potion
                    case 34440: return Item; // Mad Alchemist's Potion	
                    case 40067: return Item; // Icy Mana Potion
                    case 31677: return Item; // Fel Mana Potion
                    case 31840: return Item; // Major Combat Mana Potion
                    case 31841: return Item; // Major Combat Mana Potion
                    case 31854: return Item; // Major Combat Mana Potion
                    case 31855: return Item; // Major Combat Mana Potion
                    case 32783: return Item; // Blue Ogre Brew
                    case 32909: return Item; // Blue Ogre Brew Special
                    case 23823: return Item; // Mana Potion Injector
                    case 33093: return Item; // Mana Potion Injector
                    case 22832: return Item; // Super Mana Potion
                    case 32762: return Item; // Rulkster's Brain Juice
                    case 32902: return Item; // Bottled Nethergon Energy
                    case 32903: return Item; // Cenarion Mana Salve
                    case 32948: return Item; // Auchenai Mana Potion
                    case 43530: return Item; // Argent Mana Potion
                    case 28101: return Item; // Unstable Mana Potion
                    case 33935: return Item; // Crystal Mana Potion
                    case 18253: return Item; // Major Rejuvenation Potion
                    case 13444: return Item; // Major Mana Potion
                    case 17351: return Item; // Major Mana Draught
                    case 13443: return Item; // Superior Mana Potion
                    case 18841: return Item; // Combat Mana Potion
                    case 12190: return Item; // Dreamless Sleep Potion
                    case 17352: return Item; // Superior Mana Draught
                    case 6149: return Item; // Greater Mana Potion
                    case 3827: return Item; // Mana Potion
                    case 3385: return Item; // Lesser Mana Potion	
                    case 2455: return Item; // Minor Mana Potion
                    case 2456: return Item; // Minor Rejuvenation Potion
                    case 3087: return Item; // Mug of Shimmer Stout
                }
            }
            return null;
        }
        #endregion
    }
}
